//
//  EditViewController.m
//  Memory
//
//  Created by lanou3g on 15-3-31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "EditViewController.h"
#import "ColorMatrix.h"
#import "ImageUtil.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
@interface EditViewController ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>


@property (strong, nonatomic) UIButton *backButton;
@property (strong, nonatomic) UIImage *player;
@property (strong, nonatomic) UIImageView *playerLayer;
@property (strong, nonatomic) UIButton *playButton;
@property (strong, nonatomic) UIButton *saveButton;
@property (strong, nonatomic) AVPlayerItem *playerItem;
@property (strong, nonatomic) UIScrollView *scrollView;

@end

@implementation EditViewController
@synthesize image = image, delegate = delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor colorWithRed:16 / 255.0f green:16 / 255.0f blue:16 / 255.0f alpha:1.0f];
    self.backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 35, 35)];
    [_backButton setImage:[UIImage imageNamed:@"record_close_normal.png"] forState:UIControlStateNormal];
    [_backButton setImage:[UIImage imageNamed:@"record_close_highlighted.png"] forState:UIControlStateHighlighted];
    [_backButton addTarget:self action:@selector(pressBackButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backButton];
    
    self.saveButton = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_SIZE.width - 45, 5, 35, 35)];
    //    [self.saveButton setTitle:@"Save" forState:UIControlStateNormal];
    [_saveButton setImage:[UIImage imageNamed:@"iconfont-dui-2.png"] forState:UIControlStateNormal];
    [_saveButton setImage:[UIImage imageNamed:@"iconfont-dui-4 .png"] forState:UIControlStateHighlighted];
    [self.saveButton addTarget:self action:@selector(pressSaveButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.saveButton];
    
    [self initPlayLayer];

    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(avPlayerItemDidPlayToEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

- (void)initPlayLayer
{
    self.playerLayer = [[UIImageView alloc] init];
    _playerLayer.frame = CGRectMake(0, 44, DEVICE_SIZE.width, DEVICE_SIZE.width + 44);
    _playerLayer.userInteractionEnabled = NO;
    _playerLayer.image = image;
    self.imageNon = image;
    [self.view addSubview:_playerLayer];
    
    NSArray *arr = [NSArray arrayWithObjects:@"原图",@"LOMO",@"黑白",@"复古",@"哥特",@"锐色",@"淡雅",@"酒红",@"青柠",@"浪漫",@"光晕",@"蓝调",@"梦幻",@"夜色", nil];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, DEVICE_SIZE.width + 114, self.view.frame.size.width, self.view.frame.size.height - DEVICE_SIZE.width - 94)];
    self.scrollView.delegate = self;
    float x = 0.0 ;
    for(NSInteger i = 0; i < 14; i ++)
    {
        x = 10 + (DEVICE_SIZE.width / 4 + 10) * i;
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setImageStyle:)];
        recognizer.numberOfTouchesRequired = 1;
        recognizer.numberOfTapsRequired = 1;
        recognizer.delegate = self;
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, DEVICE_SIZE.width / 4 + 13, DEVICE_SIZE.width / 4, 23)];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setText:[arr objectAtIndex:i]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setFont:[UIFont systemFontOfSize:13.0f]];
        [label setTextColor:[UIColor whiteColor]];
        [label setUserInteractionEnabled:YES];
        [label setTag:i];
        [label addGestureRecognizer:recognizer];
        [_scrollView addSubview:label];
        
        UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, 10, DEVICE_SIZE.width / 4, DEVICE_SIZE.width / 4)];
        
        [bgImageView setTag:i];
        [bgImageView addGestureRecognizer:recognizer];
        [bgImageView setUserInteractionEnabled:YES];
        UIImage *bgImage = [self changeImage:i imageView:nil];
        bgImageView.image = bgImage;
        [_scrollView addSubview:bgImageView];

    }
    _scrollView.contentSize = CGSizeMake(x + (DEVICE_SIZE.width / 4 + 10) * 4 + 10, 0);

    [self.view addSubview:_scrollView];
}

- (void)setImageStyle:(UITapGestureRecognizer *)sender
{
    image = [self changeImage:sender.view.tag imageView:nil];
    [_playerLayer setImage:image];
}

- (UIImage *)changeImage:(int)index imageView:(UIImageView *)imageView
{
    UIImage *imageU;
    switch (index) {
        case 0:
        {
            return self.imageNon;
        }
            break;
        case 1:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_lomo];
        }
            break;
        case 2:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_heibai];
        }
            break;
        case 3:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_huajiu];
        }
            break;
        case 4:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_gete];
        }
            break;
        case 5:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_ruise];
        }
            break;
        case 6:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_danya];
        }
            break;
        case 7:
        {
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_jiuhong];
        }
            break;
        case 8:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_qingning];
        }
            break;
        case 9:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_langman];
        }
            break;
        case 10:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_guangyun];
        }
            break;
        case 11:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_landiao];
            
        }
            break;
        case 12:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_menghuan];
            
        }
            break;
        case 13:
        {
            imageU = self.imageNon;
            imageU= [ImageUtil imageWithImage:image withColorMatrix:colormatrix_yese];
            
        }
    }
    return imageU;
}

- (void)imageFitlerProcessDone:(UIImage *)imageV //图片处理完
{
    [_playerLayer setImage:imageV];
}

//- (void)pressPlayButton:(UIButton *)button
//{
//    [_playerItem seekToTime:kCMTimeZero];
//    [_player play];
//    _playButton.alpha = 0.0f;
//}

- (void)pressBackButton:(UIButton *)button
{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

// 保存的方法
- (void) pressSaveButton:(UIButton *)button
{
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    ALAssetsLibrary *assetsLibrary=[[ALAssetsLibrary alloc]init];
    [assetsLibrary writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"图片保存完毕" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark - PlayEndNotification
- (void)avPlayerItemDidPlayToEnd:(NSNotification *)notification
{
    if ((AVPlayerItem *)notification.object != _playerItem) {
        return;
    }
    [UIView animateWithDuration:0.3f animations:^{
        _playButton.alpha = 1.0f;
    }];
}

- (void)dealloc
{
    _scrollView = nil;
    _playerLayer = nil;
    image  =nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
